package java复习202408.F_多线程验证;

import org.junit.jupiter.api.Test;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.function.Function;

public class f1_多线成的创建方式验证 {

    public class Sub1Thread extends Thread{
        @Override
        public void run() {
            System.out.println("这里是子线程：" + Thread.currentThread().getName());
            try {
                sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("子线程执行结束：" + Thread.currentThread().getName());
        }
    }

    // 实现多线程的方式2：实现runnable接口，重写run方法，将runnable接口匿名实现对象或者子类实现对象传入Thread构造器
    @Test
    public void test3() throws InterruptedException, ExecutionException {
        System.out.println("主线程开始执行················");

        FutureTask futureTask = new FutureTask(new Callable() {
            @Override
            public Object call() throws Exception {
                System.out.println("这里是子线程：" + Thread.currentThread().getName());
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("子线程执行结束：" + Thread.currentThread().getName());

                return "success";
            }
        });

        Thread thread = new Thread(futureTask);
        thread.start();

        System.out.println("主线程进入睡眠3s················");
        Thread.sleep(5000);
        System.out.println("主线程睡眠结束················");


        System.out.println("主线程获取异步任务执行返回值：" + futureTask.get());
        System.out.println("主线程执行结束················");

    }

    // 实现多线程的方式2：实现runnable接口，重写run方法，将runnable接口匿名实现对象或者子类实现对象传入Thread构造器
    @Test
    public void test2() throws InterruptedException {
        System.out.println("主线程开始执行················");

        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                System.out.println("这里是子线程：" + Thread.currentThread().getName());
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("子线程执行结束：" + Thread.currentThread().getName());
            }
        };

        Thread thread = new Thread(runnable);
        thread.start();

        System.out.println("主线程进入睡眠3s················");
        Thread.sleep(5000);
        System.out.println("主线程睡眠结束················");
        System.out.println("主线程执行结束················");

    }

    // 实现多线程的方式1：继承Thread类，重写run方法
    @Test
    public void test() throws InterruptedException {
        System.out.println("主线程开始执行················");
        Sub1Thread sub1Thread = new Sub1Thread();
        sub1Thread.start();
        System.out.println("主线程进入睡眠3s················");
        Thread.sleep(5000);
        System.out.println("主线程睡眠结束················");
        System.out.println("主线程执行结束················");
    }



}
